package com.library.servlet;

import com.library.util.DBUtil;
// import com.example.library.util.PasswordUtil; // 移除此行
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.json.JSONObject;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        
        JSONObject jsonResponse = new JSONObject();
        
        try (Connection conn = DBUtil.getConnection()) {
            // 检查用户名或邮箱是否已存在
            String checkSql = "SELECT COUNT(*) FROM users WHERE username = ? OR email = ?";
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setString(1, username);
                checkStmt.setString(2, email);
                try (java.sql.ResultSet rs = checkStmt.executeQuery()) {
                    if (rs.next()) {
                        int count = rs.getInt(1);
                        if (count > 0) {
                            jsonResponse.put("success", false);
                            jsonResponse.put("message", "用户名或邮箱已存在");
                            response.setContentType("application/json");
                            response.setCharacterEncoding("UTF-8");
                            response.getWriter().write(jsonResponse.toString());
                            return;
                        }
                    }
                }
            }
            
            // 插入新用户
            String insertSql = "INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'user')";
            try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
                insertStmt.setString(1, username);
                insertStmt.setString(2, email);
                insertStmt.setString(3, password); // 直接存储明文密码
                
                int result = insertStmt.executeUpdate();
                if (result > 0) {
                    jsonResponse.put("success", true);
                    jsonResponse.put("message", "注册成功");
                } else {
                    jsonResponse.put("success", false);
                    jsonResponse.put("message", "注册失败");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResponse.put("success", false);
            jsonResponse.put("message", "系统错误，请稍后重试");
        }
        
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(jsonResponse.toString());
    }
} 